package com.atguigu.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @Author:hx
 * @DateTime:2022/9/20 19:54
 * @Description:
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    @DubboReference
    private RoleService roleService;
    @DubboReference
    private PermissionService permissionService;

    /**
     * @Description 查询所有角色信息
     * @param model:
     * @return: java.lang.String
     */
    public String findAll(Model model){
        List<Role> list = roleService.findAll();
        model.addAttribute("list",list);
        return toIndex("role");
    }

    @PreAuthorize("hasAnyAuthority('role.create')")
    @RequestMapping("/create")
    public String create(){
        return "role/create";
    }

    /**
     * @Description 添加角色
     * @param role:
     * @param model:
     * @return: java.lang.String
     */
    @PreAuthorize("hasAnyAuthority('role.create')")
    @RequestMapping("/save")
    public String insert(Role role, Model model){
        roleService.insert(role);
        return successPage(model,"添加角色成功!");
    }

    /**
     * @Description 分页查询角色信息
     * @return: java.lang.String
     */
    @RequestMapping
    @PreAuthorize("hasAnyAuthority('role.show')")
    public String findByPage(@RequestParam Map<String,Object> filters,Model model){
        PageInfo<Role> page = roleService.findByPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        return toIndex("role");
    }

    /**
     * @Description 根据id查询角色信息并跳转到修改页面
     * @param id:
     * @param model:
     * @return: java.lang.String
     */
    @RequestMapping("/edit/{id}")
    @PreAuthorize("hasAnyAuthority('role.show')")
    public String edit(@PathVariable Long id,Model model){
        Role role = roleService.getById(id);
        model.addAttribute("role",role);
        return "role/edit";
    }

    /**
     * @Description 修改角色信息
     * @param model:
     * @return: java.lang.String
     */
    @RequestMapping("/update")
    @PreAuthorize("hasAnyAuthority('role.edit')")
    public String update(Role role,Model model){
        roleService.update(role);
        return successPage(model,"添加角色成功!");
    }

    /**
     * @Description 删除角色信息
     * @param id:
     * @return: java.lang.String
     */
    @RequestMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('role.delete')")
    public String delete(@PathVariable Long id){
        roleService.delete(id);
        return "redirect:/role";
    }

    @PreAuthorize("hasAnyAuthority('role.assgin')")
    @RequestMapping("/assignShow/{roleId}")
    public String assignShow(@PathVariable Long roleId,Model model){
        model.addAttribute("roleId",roleId);
        List<Map<String,Object>> zNodes = roleService.findPermissionByRoleId(roleId);
        model.addAttribute("zNodes", JSON.toJSONString(zNodes));
        return "role/assignShow";
    }

    @PreAuthorize("hasAnyAuthority('role.assgin')")
    @RequestMapping("/assignPermission")
    public String assignPermission(Model model,Long roleId,@RequestParam List<Long> permissionIds){
        roleService.assignPermission(roleId,permissionIds);
        return successPage(model,"分配权限成功!");
    }
}
